﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BBICMS.Model.Validation
{
    public class UrlAttribute    : RegularExpressionAttribute, IClientValidatable
    {
        public UrlAttribute()
            : base(@"^(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$")
        {
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var errorMessage = FormatErrorMessage(metadata.GetDisplayName());

            yield return new UrlValidationRule(errorMessage);
        }
    }
    public class UrlValidationRule : ModelClientValidationRule
    {
        public UrlValidationRule(string errorMessage)
        {
            ErrorMessage = errorMessage;
            ValidationType = "url";
        }
    }
}

